\documentclass{article}
\title{Extending \LaTeX}
\author{Jack Fang}
\begin{document}
\maketitle
\LaTeX{} lets you write new commands (or adapt old ones), create or adapt environments, and write your own packages and even classes. This document will give detailed descriptions on how to do simple things, but won't cover all the issues relating to class creation. 
\section{Simple Macros}
Sometimes you might want to repeat a long expression. The tidy way to do this is to use $\backslash$def (or $\backslash$newcommand). The following defines $\backslash$xdt to produce something like X.desktop
\begin{verbatim}
   \def \xdt{$\cal X\!\!$\texttt{.desktop}}
\end{verbatim}
One problem that can arise is that your new code can introduce unwanted space. The Spaces in macros section of the FAQ covers this issue. 
\section{Commands}
Use $\backslash$newcommand to create a command and $\backslash$renewcommand to over-write an existing one. You can use only letters in the names of commands.
\subsubsection{\textbf{$\backslash$newcommand}}
A completely new command can be created using
\begin{verbatim}
   \newcommand{\commandname}[number of arguments]
                           {command text, using #1, #2 etc to denote arguments}
\end{verbatim}
For example,
\begin{verbatim}
   \newcommand{\ve}[1]{\(#1_1 ... #1_n\)}
   \ve{x}
\end{verbatim}
produces as output $x_1$ ... $x_n$. A problem with this example is that it shouldn't change to math mode if LaTeX is already in that mode. A better try would be
\begin{verbatim}
   \newcommand{\ve}[1]{\ensuremath{#1_1 ... #1_n}}
\end{verbatim}
which will only change to math mode if it's necessary.

The following weekday macro uses the ifthen package. Note the use of ``\%'' characters to suppress spaces in the final output. weekday\{3\} should print out Wednesday.
\begin{verbatim}
   \usepackage{ifthen}
   \newcommand{\weekday}[1]%
   {%
   \ifthenelse{\equal{#1}{1}}{Monday}{}%
   \ifthenelse{\equal{#1}{2}}{Tuesday}{}%
   \ifthenelse{\equal{#1}{3}}{Wednesday}{}%
   \ifthenelse{\equal{#1}{4}}{Thursday}{}%
   \ifthenelse{\equal{#1}{5}}{Friday}{}%
   \ifthenelse{\equal{#1}{6}}{Saturday}{}%
   \ifthenelse{\equal{#1}{7}}{Sunday}{}%
   }

\weekday{3}
\end{verbatim}
\subsubsection{$\backslash$renewcommand}
Let's start with some short though useful examples. Suppose you wanted to have \emph{References} in a book rather than \emph{Bibliography}. If you look in book.cls you'll see 
\begin{verbatim}
   \newcommand\bibname{Bibliography}
\end{verbatim}
so adding
\begin{verbatim}
   \renewcommand{\bibname}{References}
\end{verbatim}
to your file should achieve what you want. Counters (e.g. figure - a variable that stores the figure number) have related commands (e.g. thefigure) to control their appearance, so they're easy to customise
\begin{verbatim}
   \renewcommand\thefigure{\roman{figure}}
\end{verbatim}
produces figure numbers in lower case roman numerals. Longer commands can be adapted too. Remember however, that if the command involves a @ character you have to enclose your changes in $\backslash$makeatletter \dots{} $\backslash$makeatother or put the code into a package. Here's an example that changes the appearance of section headings, making them into centred small caps -
\begin{verbatim}
   \makeatletter
   \renewcommand{\section}{\@startsection{section}{1}{0mm}
   {\baselineskip}%
   {\baselineskip}{\normalfont\normalsize\scshape\centering}}%
   \makeatother
   \begin{document}
\end{verbatim}
The following's adapted from the \TeX{} Frequently Asked Questions - Rather than overwriting an old command it's common to want to add some code at the beginning or the end of it. Suppose we want a version of a command that does some small extension of its original definition: we might try:
\begin{verbatim}
   \renewcommand{\splat}{addedcode\splat}
\end{verbatim}
However, this would not work: a call to $\backslash$splat would execute addedcode, and then call the redefined $\backslash$splat again; this is an infinite recursive loop. Fortunately, the \TeX{} primitive $\backslash$let command comes to our rescue; it allows us to take a ``snapshot'' of the current state of a command, which we can then use in the redefinition of the command. So:
\begin{verbatim}
   \let\Oldsplat\splat
   \renewcommand{\splat}{addedcode\Oldsplat}
\end{verbatim}
effects the required patch, safely. Adding things at the end of a command works similarly.
If $\backslash$splat takes arguments, one must pass them on:
\begin{verbatim}
   \renewcommand{\splat}[2]{addedcode\Oldsplat{#1}{#2}}
\end{verbatim}
\section{Environments}
A new environment is slightly more difficult to create, because you can define what you want to happen on entering and leaving the environment.
\begin{verbatim}
   \newenvironment {\ environmentname\}[number of arguments]
                                       [default value of the 1st (optional) argument]
                                       {entry code, using #1, #2 etc to denote arguments}
                                       {exit code - arguments can't be used}
\end{verbatim}
Simple use isn't as painful as it looks - the following provides a variant of the itemize command, emphasising the items.
\begin{verbatim}
   \newenvironment{emlist}{\begin{itemize} \em}{\end{itemize}}
   \begin{emlist}
   \item first comment
   \item second comment
   \end{emlist}
   The end of the environment ends the scope of the emphasis.
\end{verbatim}
produces
\begin{itemize}
\item {first comment}
\item {second comment}
\end{itemize}
The end of the environment ends the scope of the emphasis.
$\backslash$renewenvironment is used to change existing environments, but you can't change the number of arguments an environment takes.
\section{Packages}
A package needn't have any special code, but usually it starts with some special package commands
\begin{enumerate}
\item{$\backslash$NeedsTeXFormat\{\LaTeXe{}\}[1994/12/01] - means that the package requires a version of \LaTeXe{} dating no further back than 1994/12/01.}
\item{$\backslash$ProvidesPackage\{amsmath\}[2000/07/18 v2.13 AMS math features] - defines the package name (in this case amsmath) and a string to be printed out when the package is used.}
\item{$\backslash$RequirePackage\{amssymb,mathptm\} - tries to load in any prerequisite packages if they haven't been loaded in already}
\item{$\backslash$DeclareOption\{intlimits\}\{$\backslash$let$\backslash$ilimits@$\backslash$displaylimits\} - creates and defines an option for the package}
\item{$\backslash$DeclareOption\{nonamelimits\}\{$\backslash$PassOptionsToPackage\{nonamelimits\}\{amsopn\}\} - creates an option, in this case passing the option onto another package (but you have to load the package later)}
\item{$\backslash$ExecuteOptions\{nointlimits,sumlimits,namelimits,centertags\} - executes some default options}
\item{$\backslash$ProcessOptions - processes the code of each option that was both specified and declared}
\end{enumerate}
 
When writing a package you should try to minimize name-clashes 
"	Prefix each variable and command name by the package name
"	Use @ in the name of internal variables so that users can't access them directly
Useful utilities from builting packages include
\begin{itemize}
    \item{ifthen}
	 \item{calc}
	 \item{keyval}
	 \item{somedefs}
\end{itemize}
\section{Classes}
Creating a class file needs more work than package creation does. A class file can use many of the commands mentioned in the Packages section. In addition it can use
\begin{enumerate}
\item{$\backslash$ProvidesClass\{handout\} - to define its name.}
\item{$\backslash$LoadClass[12pt,twoside,openright]\{book\} - to load in an existing class (one wouldn't normally write a class file from scratch).}
\end{enumerate}
\section{See Also}
See
\begin{enumerate}
\item{\LaTeXe{} for class and package writers}\\
A complete set of the documented source of \LaTeX{} may be prepared by processing the source2e.tex file
\item{classes.dtx}\\produces the standard classes other than letter, and may itself be formatted with \LaTeX
\item{ltxguide.cls}\\How to package your \LaTeX{} file
\item{\TeX{} Frequently Asked Questions}\\(see the "Macro programming" section).for more details.
\end{enumerate}
\end{document}
